$import( "SmitearWebGL.Objects.Parser.Collada.DaeEntity" );
$import( "SmitearWebGL.Objects.Parser.Collada.DaeSource" );
$import( "SmitearWebGL.Objects.Parser.Collada.DaeInput" );
$import( "SmitearWebGL.Core.Structure.Map" );

SmitearWebGL.Objects.Parser.Collada.DaePrimitive = SmitearWebGL.Objects.Parser.Collada.DaeEntity.extend(
{
	/******** constructor ********/

	init : function( document, mesh, node )
	{
		this.mesh = mesh;
		this.count = 0;
		this.material = "";
		this.vcount = null;
		this.type = "";
		this.polygons = null;
		this._inputs = null;
		this._sources = null;

		this._super( document, node );
		this._registClass( "SmitearWebGL.Objects.Parser.Collada.DaePrimitive" );
	}
	,
	read : function( node )
	{
		$THROW( this.checkNode(node), "expected a primitive element!" );
		$THROW( this.mesh, "parent-element 'mesh' or 'convex_mesh' not set!" );

		this._super( node );

		this.type =  node.localName;

		this.count = this.getAttributeAsInt( node, SmitearWebGL.Objects.Parser.Collada.Const.DAE_COUNT_ATTRIBUTE );
		this.material = this.getAttribute( node, SmitearWebGL.Objects.Parser.Collada.Const.DAE_MATERIAL_ATTRIBUTE );
		this.vcount = [];
		this.polygons = [];

		if( this.count == 0 )
		{
			return;
		}

		this._inputs = new SmitearWebGL.Core.Structure.Map();
		this._sources = new SmitearWebGL.Core.Structure.Map();

		var parent = node.parentNode;

		switch( parent.localName )
		{
			case SmitearWebGL.Objects.Parser.Collada.Const.DAE_MESH_ELEMENT:
				switch( node.localName )
				{
					case SmitearWebGL.Objects.Parser.Collada.Const.DAE_POLYGONS_ELEMENT:
						this.parsePolygons( node );
						break;
						
					default:
						this.parse( node );
						break;
				}
				break;
				
			case SmitearWebGL.Objects.Parser.Collada.Const.DAE_CONVEX_MESH_ELEMENT:
				break;
				
			default:
				break;
		}
	}
	,
	parse : function( node )
	{
		var p = this.getInts( this.getNode( node, SmitearWebGL.Objects.Parser.Collada.Const.DAE_POLYGON_ELEMENT ) );
		var vcountNode = this.getNode( node, SmitearWebGL.Objects.Parser.Collada.Const.DAE_VERTEXCOUNT_ELEMENT );
		var inputList = this.getNodeList( node, SmitearWebGL.Objects.Parser.Collada.Const.DAE_INPUT_ELEMENT );
		var inputs = [];
		var input;
		var maxoffset = 0;
		var source;

		if( vcountNode && vcountNode.hasAttribute != undefined )
		{
			this.vcount = this.getInts( vcountNode );
		}
		
		for ( var i = 0; i < inputList.length; ++ i )
		{
			input = new SmitearWebGL.Objects.Parser.Collada.DaeInput( this.document, inputList[i] );
			maxoffset = Math.max( maxoffset, input.offset + 1 );
			inputs.push( input );

			if ( this.mesh.vertices.id == input.source )
			{
				this.document.sources[input.source] = this.mesh.vertices.source;
			}

			this._inputs.add( input, [] );
			this._sources.add( input.semantic, this.document.sources[input.source] );
		}

		for( var i = 0, j = 0; i < p.length; i += maxoffset, j ++ )
		{
			for ( item in inputs )
			{
				var input = inputs[item];
				var idx = p[i + input.offset];

				source = this.document.sources[input.source];
				this._inputs.item( input ).push( idx );
			}
		}
	}
	,
	parsePolygons : function( node )
	{
		var inputs = [];
		var input;
		var maxoffset = 0;
		var inputList = this.getNodeList( node, SmitearWebGL.Objects.Parser.Collada.Const.DAE_INPUT_ELEMENT );
		var pList = this.getNodeList( node, SmitearWebGL.Objects.Parser.Collada.Const.DAE_POLYGON_ELEMENT );
		var i;

		for( i = 0; i < inputList.length; i ++ )
		{
			input = new SmitearWebGL.Objects.Parser.Collada.DaeInput(this.document, inputList[i]);
			maxoffset = Math.max( maxoffset, input.offset + 1 );
			inputs.push( input );

			if(this.mesh.vertices.id == input.source)
			{
				this.document.sources[input.source] = this.mesh.vertices.source;
			}

			this._inputs.add( input, [] );
			this._sources.add( input.semantic, this.document.sources[input.source] );
		}

		for ( var i = 0; i < pList.length; ++ i )
		{
			var p = this.getInts( pList[i] );
			var poly = [];
			
			for( var i = 0, j = 0; i < p.length; i += maxoffset, j ++ )
			{
				//this._inputs.indices.push( j );
				for ( item in inputs )
				{
					var input = inputs[ item ];
					var idx = p[i + input.offset];
					this._inputs.item( input ).push( this.document.sources[input.source].values[idx] );
				}
			}
			this.polygons.push( poly );
		}
	}
	,
	checkNode : function( node )
	{
		var name = node.localName;

		return (name == SmitearWebGL.Objects.Parser.Collada.Const.DAE_TRIANGLES_ELEMENT ||
		name == SmitearWebGL.Objects.Parser.Collada.Const.DAE_TRIFANS_ELEMENT ||
		name == SmitearWebGL.Objects.Parser.Collada.Const.DAE_TRISTRIPS_ELEMENT ||
		name == SmitearWebGL.Objects.Parser.Collada.Const.DAE_LINESTRIPS_ELEMENT ||
		name == SmitearWebGL.Objects.Parser.Collada.Const.DAE_LINES_ELEMENT ||
		name == SmitearWebGL.Objects.Parser.Collada.Const.DAE_POLYGONS_ELEMENT ||
		name == SmitearWebGL.Objects.Parser.Collada.Const.DAE_POLYLIST_ELEMENT);
	}
//	,
//	getIndices : function()
//	{
//		return this._inputs.indices;
//	}
	,
	getNormals : function()
	{
		return this.getFirstInput( "NORMAL" );
	}
	,
	getVertices : function()
	{
		return this.getFirstInput( "VERTEX" );
	}
	,
	getTexCoords : function( setID )
	{
		return this.getInputBySet( "TEXCOORD", setID );
	}
	,
	getSource : function( semantic )
	{
		var daeInput;
		var iter = this._sources.iterator;
		for ( iter.begin(); iter.end(); iter.next() )
		{
			daeInput = iter.item().key;
			if( daeInput == semantic )
			return iter.item().value.values;
		}
		return null;
	}
	,
	getFirstInput : function( semantic )
	{
		var daeInput;
		var iter = this._inputs.iterator;
		for ( iter.begin(); iter.end(); iter.next() )
		{
			daeInput = iter.item().key;
			if( daeInput.semantic == semantic )
			return iter.item().value;
		}
		return null;
	}
	,
	getInputBySet : function( semantic, setID )
	{
		if( this.getInputCount(semantic) == 1 )
			return this.getFirstInput( semantic );
		
		var daeInput;
		var iter = this._inputs.iterator;
		for ( iter.begin(); iter.end(); iter.next() )
		{
			daeInput = iter.item().key;
			if( daeInput.semantic == semantic && daeInput.setId == setID )
			return iter.item().value;
		}
		return [];
	}
	,
	getInputCount : function( semantic )
	{
		var daeInput;
		var cnt = 0;
		var iter = this._inputs.iterator;
		for ( iter.begin(); iter.end(); iter.next() )
		{
			daeInput = iter.item().key;
			if( daeInput.semantic == semantic )
			cnt++;
		}
		return cnt;
	}
}
);